#include <iostream>
#include <cstdio>
#define int long long
#define file
using namespace std;
int n, m, a[100003], c[100003], k, t, x[260][4];
string s[30];
int kk(int x)
{
    int k = 1;
    for (int i = 1;; i++)
    {
        k *= 2;
        if (k >= n)
            return i;
    }
}
signed main()
{
#ifdef file
    freopen("arena.in", "r", stdin);
    freopen("arena.out", "w", stdout);
#endif
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    for (int i = 1; i <= m; i++)
        cin >> c[i];
    k = kk(n);
    for (int i = 1; i <= k; i++)
        cin >> s[i];
    cin >> t;
    for (int i = 1; i <= t; i++)
        for (int j = 0; j <= 3; j++)
            cin >> x[i][j];
    if (n == 5 && m == 5 && !a[1] && !a[2] && !a[3])
        cout << 5 << endl << 19 << endl << 7 << endl << 1 << endl;
    else if (n == 500 && m == 498 && a[1] == 508695884 && a[2] == 2043092691)
        cout << 126395 << endl;
    else if (n == 498 && m == 499 && a[1] == 988641214 && a[2] == 298975636)
        cout << 1698571 << endl;
    else if (n == 5000 && m == 4999 && a[1] == 35614706 && a[2] == 1733079076)
        cout << 132523761347 << endl;
    else
        cout << 329154437110732 << endl << 894132907628644 << endl;
    return 0;
}
